package com.zywl.utils.utils;

import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;



public class Base64ToMultipart {

    /**
     * 使用 java.util.Base64.Decoder和java.util.Base64.Encoder的JDK公共API（推荐）
     * base64转Multipartfile
     * @param base64
     * @return
     */
    public static MultipartFile base64ToMultipart(String base64) {
        try {
            String[] baseStrs = base64.split(",");
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] b = decoder.decode(baseStrs[1]);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {
                    b[i] += 256;
                }
            }
            return new BASE64DecodedMultipartFile(b, baseStrs[0]);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


}